package com.ruoyi.activiti.controller;

import com.github.pagehelper.Page;
import com.ruoyi.activiti.domain.LeaveBean;
import com.ruoyi.activiti.domain.dto.LeaveBeanDto;
import com.ruoyi.activiti.domain.vo.*;
import com.ruoyi.activiti.service.ILeaveBeanService;
import com.ruoyi.activiti.service.TaskTodoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/task")
public class TaskController extends BaseController {

    @Autowired
    private TaskTodoService taskTodoService;


    /**
     * 查询我的待办
    * */
    @GetMapping("/list")
    public TableDataInfo todoTask(LeaveBeanDto leaveBeanDto) {
        List<TodoVo> todoVos = taskTodoService.todoTask(leaveBeanDto);
        return getDataTable(todoVos);
    }

    /**
    * 请假申请我的已办
    * */
    @GetMapping("/doneList")
    public TableDataInfo doneLeave() {
        Page<TodoDoneVo> todoDoneVos = taskTodoService.doneLeave();
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setTotal(todoDoneVos.getTotal());
        tableDataInfo.setRows(todoDoneVos);
        return tableDataInfo;
    }
    /**
     * 处理任务
    * */
    @PostMapping
    public AjaxResult handleTask(@RequestBody TodoHandleVo todoHandleVo) {
        taskTodoService.handleTask(todoHandleVo);
        return AjaxResult.success("处理成功");
    }

    /**
     * 查看流程图和和节点高亮
    * */
    @GetMapping("/getHighlight")
    public AjaxResult getActivitiProcessHighlight(String procDefId,String instanceId) {
        if(StringUtils.isBlank(procDefId)){
            throw new RuntimeException("流程定义Id不能为空");
        }
        if(StringUtils.isBlank(instanceId)){
            throw new RuntimeException("流程实例Id不能为空");
        }
        ProcessHighlightEntity highlight = taskTodoService.processViewer(procDefId, instanceId);
        return AjaxResult.success(highlight);
    }
    /**
     * 查询某个申请历史审批记录
    * */
    @GetMapping("/history/{instanceId}")
    public AjaxResult getInstanceDoneHistory(@PathVariable("instanceId") String instanceId) {
        List<TodoDoneVo> historyVos = taskTodoService.selectHistoryList(instanceId);
        return AjaxResult.success(historyVos);
    }

}
